package com.tz.test.controller;

import com.tz.test.entity.ScoreDTO;
import com.tz.test.service.InsertDataHouseService;
import com.tz.test.service.impl.MultiThreadTransactionManager;
import com.tz.test.service.impl.MultiThreadTransactionServiceV1Impl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cmd
 * @data 2020/4/10 19:54
 */
@Slf4j
@RestController
public class SaveDataController {

    @Autowired
    private MultiThreadTransactionServiceV1Impl threadTransactionalService;

    @Autowired
    private InsertDataHouseService<ScoreDTO> insertDataHouseService;




    @GetMapping("/test/{size}")
    public boolean createUser(@PathVariable Integer size) {
        if (size < 1) {
            log.warn("createUser param is error");
            return false;
        }
        List<ScoreDTO> userInfos = new ArrayList<>();
        for (int i = 1; i <= size; i++) {
            ScoreDTO scoreDTO = new ScoreDTO();
            scoreDTO.setExamId(1);
            scoreDTO.setSubjectId(1);
            scoreDTO.setRecordId(i);
            scoreDTO.setConfigId(i);
            scoreDTO.setStudentScore(2.0);
            userInfos.add(scoreDTO);
        }

        for (int j = 1; j <= size; j++) {
            ScoreDTO scoreDTO = new ScoreDTO();
            scoreDTO.setExamId(1);
            scoreDTO.setSubjectId(1);
            scoreDTO.setRecordId(j);
            scoreDTO.setConfigId(j);
            scoreDTO.setStudentScore(2.0);
            scoreDTO.setStudentAnswer("aa");
            userInfos.add(scoreDTO);
        }

        long start = System.currentTimeMillis();
        threadTransactionalService.handlerPartitionData(userInfos);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        return true;
    }

    @GetMapping("/testV2/{size}")
    public boolean testV2(@PathVariable Integer size) {
        if (size < 1) {
            log.warn("createUser param is error");
            return false;
        }
        List<ScoreDTO> userInfos = new ArrayList<>();
        for (int i = 1; i <= size; i++) {
            ScoreDTO scoreDTO = new ScoreDTO();
            scoreDTO.setExamId(1);
            scoreDTO.setSubjectId(1);
            scoreDTO.setRecordId(i);
            scoreDTO.setConfigId(i);
            scoreDTO.setStudentScore(2.0);
            userInfos.add(scoreDTO);
        }

        for (int j = 1; j <= size; j++) {
            ScoreDTO scoreDTO = new ScoreDTO();
            scoreDTO.setExamId(1);
            scoreDTO.setSubjectId(1);
            scoreDTO.setRecordId(j);
            scoreDTO.setConfigId(j);
            scoreDTO.setStudentScore(2.0);
            scoreDTO.setStudentAnswer("aa");
            userInfos.add(scoreDTO);
        }

        MultiThreadTransactionManager<ScoreDTO> scoreDTOMultiThreadTransactionServiceImplV2
                = new MultiThreadTransactionManager<>(insertDataHouseService);

        long start = System.currentTimeMillis();
        scoreDTOMultiThreadTransactionServiceImplV2.handlerPartitionData(userInfos);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
        return true;
    }
}
